﻿namespace EFS.RC.CCAdmin.Surveys.Category
{
	/// Class Edit
	public partial class Edit : EFS.RC.CCAdmin.Common.Control
	{
		#region Locals
		/// host
		private EFS.Common.UI.Controls.ModalDialogHost host = null;

		/// surveyId
		private System.Int32 surveyId = 0;
		#endregion

		#region Constructors
		/// Implicit
		public Edit()
		{
			InitializeComponent();
		}

		/// Explicit
		public Edit(
			EFS.Common.UI.Controls.ModalDialogHost host,
			System.Int32 surveyId,
			System.Int32? categoryID)
		{
			// check
			EFS.Common.Verify.Reference(host, "host");

			InitializeComponent();

			//set listener up
			UtilitiesService.SurveyCategory_GetCompleted += UtilitiesService_SurveyCategory_GetCompleted;

			//set new category response listener
			UtilitiesService.SurveyCategory_CreateCompleted += UtilitiesService_SurveyCategory_CreateCompleted;

			//set edit response listener
			UtilitiesService.SurveyCategory_UpdateCompleted += UtilitiesService_SurveyCategory_UpdateCompleted;

			this.host = host;
			this.host.OnButtonOKClick += OnParentOKClicked;

			// set ID
			this.CategoryID = categoryID;
			this.surveyId = surveyId;
		}
		#endregion

		#region Properties
		/// Property CategoryID: Get, Set
		public System.Int32? CategoryID { get; set;}

		/// Property IsValid: Get
		private System.Boolean IsValid
		{
			get
			{
				return EFS.Common.Verify.String(CategoryName) && Priority.HasValue;
			}
		}

		/// Property IsValid: Get
		private System.String CategoryName
		{
			get
			{
				return EFS.Common.Functions.String.GetTrimmedString(textBoxCategoryName.Text);
			}
		}

		/// Property IsValid: Priority
		private System.Int32? Priority
		{
			get
			{
				System.Int32 retValue = 0;
				if (System.Int32.TryParse(textBoxPriority.Text, out retValue))
				{
					return retValue;
				}

				// none
				return null;
			}
		}
		#endregion

		#region LoadData
		/// Load data
		private void LoadData()
		{
			// if edit
			if (CategoryID.HasValue)
			{
				// show wait DLG
				EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.InProgress(this, "Please wait while retrieving data");

				// call ws
				UtilitiesService.SurveyCategory_GetAsync(CategoryID.Value);

				surveyQuestionsList.Visibility = System.Windows.Visibility.Visible;
				return;
			}

			surveyQuestionsList.Visibility = System.Windows.Visibility.Collapsed;
		}
		#endregion

		#region WS Callbacks
		/// GetCategoryDetailsCompleted
		private void UtilitiesService_SurveyCategory_GetCompleted(object sender, WebProxy.SurveyCategory_GetCompletedEventArgs e)
		{
			// close wait DLG
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.Completed(this);

			try
			{
				// verify
				if (null != e.Error)
				{
					throw e.Error;
				}

				// deserialize
				EFS.RC.CC.CH.Entities.Surveys.Category category =
					EFS.Common.Serialization.Functions.Deserialize<EFS.RC.CC.CH.Entities.Surveys.Category>(e.Result.ToString());

				//bind/fill form
				textBoxCategoryName.Text = category.Name;
				textBoxPriority.Text = category.Priority.HasValue ? category.Priority.Value.ToString() : "";

				// load list
				surveyQuestionsList.SetCategoryId(CategoryID);
			}
			catch (System.Exception ex)
			{
				ShowErrorMessage("Failed to retrieve category. " + ex.Message);
			}
		}

		/// Create category completed
		private void UtilitiesService_SurveyCategory_CreateCompleted(object sender, WebProxy.SurveyCategory_CreateCompletedEventArgs e)
		{
			// show wait DLG
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.Completed(this);

			try
			{
				// verify
				if (null != e.Error)
				{
					throw e.Error;
				}

				this.CategoryID = e.Result;
				System.Windows.MessageBox.Show("You may now create questions in the new category.");
				LoadData();
			}
			catch (System.Exception ex)
			{
				ShowErrorMessage("Failed to create category. " + ex.Message);
			}
		}

		/// Update category completed
		private void UtilitiesService_SurveyCategory_UpdateCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
		{
			// show wait DLG
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.Completed(this);

			try
			{
				// verify
				if (null != e.Error)
				{
					throw e.Error;
				}

				// close parent
				this.host.Close(EFS.Common.UI.Controls.ModalDialogHost.ClosedReason.Ok);
			}
			catch (System.Exception ex)
			{
				ShowErrorMessage("Failed to update category. " + ex.Message);
			}
		}
		#endregion

		#region Events
		/// OnParentOKClicked
		private void OnParentOKClicked(object sender, System.Windows.RoutedEventArgs e, System.Boolean dialogResult)
		{
			// test if valid
			if (!IsValid)
			{
				return;
			}

			// create
			if (!CategoryID.HasValue)
			{
				// show wait DLG
				EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.InProgress(this, "Please wait while creating category");

				//ws call
				UtilitiesService.SurveyCategory_CreateAsync(surveyId, CategoryName, Priority.Value);
			}
			else
			{
				// show wait DLG
				EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.InProgress(this, "Please wait while updating category");

				//ws call
				UtilitiesService.SurveyCategory_UpdateAsync(CategoryID.Value, surveyId, CategoryName, Priority.Value);
			}
		}

		/// Loaded
		private void Control_Loaded(object sender, System.Windows.RoutedEventArgs e)
		{
			// ignore in design tool
			if (!System.ComponentModel.DesignerProperties.IsInDesignTool)
			{
				//load data
				LoadData();
			}
		}
		#endregion
	}

}